<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 查询条件 -->
        <el-row>
          <el-form ref="forms" :model="pageData" label-width="80px">
            <el-form-item label="关键字" prop="keyword">
              <el-col :span="6">
                <el-input
                  size="small"
                  placeholder="根据编号搜索"
                  style="width: 80%"
                  v-model="pageData.keyword"
                />
              </el-col>
              <el-col :span="18" style="text-align: right">
                <el-button size="small" @click="clears">
                  {{ $t("table.clear") }}
                </el-button>
                <el-button size="small" type="primary" @click="getlist">
                  {{ $t("table.search") }}
                </el-button>
              </el-col>
            </el-form-item>
          </el-form>
        </el-row>
        <!-- 提示表格总数量 -->
        <el-row>
          <el-alert
            :title="countTotal"
            type="info"
            show-icon
            :closable="false"
          />
        </el-row>
        <!-- 表格数据 -->
        <el-row>
          <el-table
            :header-cell-style="{ 'background-color': '#fafafa' }"
            :data="items"
          >
            <el-table-column prop="id" label="编号" width="200" />
            <el-table-column prop="questionType" label="题型" >
              <template #default="{ row }">
                {{questionTypeZH(row.questionType)}}
              </template>
            </el-table-column>
            <el-table-column prop="questionIDs" label="题目编号">
              <template #default="{ row }">
                <span
                  class="questionIDsCSS"
                  v-for="item in row.questionIDs"
                  :key="item.number"
                  @click="openPreview(item.id)"
                  >{{ item.number }}</span
                >
              </template>
            </el-table-column>
            <el-table-column prop="addTime" label="录入时间" />
            <el-table-column prop="totalSeconds" label="答题时间(s)" />
            <el-table-column prop="accuracyRate" label="正确率(%)" />
            <el-table-column prop="userName" label="录入人" />
            <el-table-column label="操作" width="80">
              <template #default="{ row }">
                <el-row>
                  <el-button
                    type="danger"
                    icon="el-icon-delete"
                    circle
                    plain
                    @click="deleteRow(row.id)"
                  ></el-button>
                </el-row>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
        <!-- 分页组件 -->
        <el-row class="pagination-css">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageData.page"
            :page-sizes="[10, 20, 30]"
            :page-size="pageData.pagesize"
            layout="prev, pager, next, sizes, jumper"
            :total="counts"
            background
          >
          </el-pagination>
        </el-row>
      </el-card>
    </div>
    <questions-preview
      ref="preview"
      :dialogVisible.sync="dialogVisible"
      :id="id"
    >
    </questions-preview>
  </div>
</template>

<script>
import { questionType as questionTypeList } from '@/api/hmmm/constants.js'
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import QuestionsPreview from '../components/questions-preview.vue'
export default {
  components: {
    QuestionsPreview
  },
  data () {
    return {
      counts: 0,
      items: [],
      pageData: {
        page: 1,
        pagesize: 20,
        keyword: null
      },
      dialogVisible: false,
      id: 0
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    // 刷新列表
    async getlist () {
      const { data } = await randoms(this.pageData)
      this.counts = data.counts
      this.items = data.items
      console.log(data)
    },
    // 分页页码变更触发事件
    handleCurrentChange (val) {
      this.pageData.page = val
      this.getlist()
    },
    // 分页展示条数变更触发事件
    handleSizeChange (val) {
      this.pageData.pagesize = val
      this.getlist()
    },
    // 清除查询条件
    clears () {
      this.$refs.forms.resetFields()
    },
    // 删除表格行数据
    deleteRow (id) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeRandoms({ id })
        this.getlist()
        this.$message.success('删除成功')
      })
    },
    openPreview (id) {
      this.dialogVisible = true
      this.id = id
      this.$nextTick(() => {
        this.$refs.preview.getDetail()
      })
    },
    questionTypeZH (questionType) {
      const questionTypeData = questionTypeList.find(item => {
        return item.value === questionType
      })
      return questionTypeData.label
    }

  },
  computed: {
    // 计算数据总条数
    countTotal () {
      return `数据一共 ${this.counts} 条`
    }
  }
}
</script>

<style  scoped>
.app-container {
  margin: 10px;
  padding: 0px;
}
.el-alert {
  margin-bottom: 15px;
}

.questionIDsCSS {
  color: rgb(0, 153, 255);
}
.pagination-css {
  margin-top: 20px;
  text-align: right;
}
</style>
